package com.console.operations.planmgr;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.common.test.GetDataFromImportFile;
import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.common.util.GetTempleteFileUtil;
import com.console.pages.planmgr.PlanBondBatchPage;

	/*
	@author 王嘉犇
	@name 计划债券批量导入
	 */

public class BondBatchImport extends OperationBase {

	public BondBatchImport(UITestBase tb){
		super(tb);
	}
	
	//导入的理财计划债权状态是否都为招标中
	public boolean isallstatuscorrected = true;
	
	public void batchimport() throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String datestr = sdf.format(date);
        Random random = new Random();
        int x = random.nextInt(8999);
        x = x+1000;
        String tempstr = datestr + String.valueOf(x);
        //得到excel里某列标题里的借款标题单元格数据，放进一个list里
		List<String> loantitlelst = GetDataFromImportFile.getMultiLineDataListFromIndex(GetTempleteFileUtil.LOANBATCH_TEMPLATE, "借款标题");
		for(int i=0;i<loantitlelst.size();i++){
			String loantitle = loantitlelst.get(i);
			//如果借款标题存在，则更新
			if(DBUtil.isQueryData("select * from fiz_loan where vc_name = ? and dc_type = ?", new Object[]{loantitle, "03"})){
				DBUtil.update("update fiz_loan set vc_name = ? where vc_name = ? and dc_type = ?", new Object[]{tempstr + "000" + (i+1), loantitle, "03"});
			}
		}
		//得到excel里某列标题里的合同编号单元格数据，放进一个list里
		List<String> constractnolst = GetDataFromImportFile.getMultiLineDataListFromIndex(GetTempleteFileUtil.LOANBATCH_TEMPLATE, "合同编号");
		for(int i=0;i<constractnolst.size();i++){
			String constractno = constractnolst.get(i);
			//如果fiz_loan_prop表里存在vc_code为contract-no且vc_value为导入的合同编号时，把合同编号更新为固定时间格式
			if(DBUtil.isQueryData("select * from fiz_loan_prop where vc_code = 'contract-no' and vc_value = ?", new Object[]{constractno})){
				DBUtil.update("update fiz_loan_prop set vc_value = ? where vc_code = 'contract-no' and vc_value = ?", new Object[]{tempstr + "000" + (i+1), constractno});
			}
		}
		switchToDefaultContent();
		PlanBondBatchPage plbp = new PlanBondBatchPage(tb);
		waitForElementExist(plbp.upLoadFile);
		//上传loanBatch_template.xls
		plbp.upLoadFile.chooseFile(GetTempleteFileUtil.LOANBATCH_TEMPLATE);
		pause(3000);
		//点击上传按钮
		plbp.upLoadButton.click();
		waitForElementExist(plbp.importButton);
		//点击导入按钮
		plbp.importButton.click();
		pause(2000);
		Set<String> statusset = new HashSet<String>();
		for(int i=0;i<constractnolst.size();i++){
			String constractno = constractnolst.get(i);
			
			String status = DBUtil.queryOneCell("select dc_status\n" +
											    "from fiz_loan\n" +
												"where pk_id = (\n" +
											    			   "select fk_loan_id\n" +
											    			   "from fiz_loan_prop\n" +
											    			   "where vc_code = ?\n" +
											    			     "and vc_value = ?\n" +
											    			  ")\n" +
											      "and dc_type = ?", new Object[]{"contract-no", constractno, "03"});
			if(!status.equals("10")){
				log.error("债券合同号 " + constractno + "对应债券状态不正确");
			}
			statusset.add(status);
		}
		//判断导入的理财计划债权状态是否都为招标中
		isallstatuscorrected = statusset.size()==1 && statusset.contains("10");
	}
}
